﻿using MusicPlayerPlus.Interface.Base;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AMing.Core.Method;
using MusicPlayerPlus.Interface.Model.Enum;

namespace MusicPlayerPlus.PlugIn.HotKey
{
    public class PlugIn : InterfaceBase,
        //Input
        MusicPlayerPlus.Interface.Base.Action.Input.ISetMainWindow,
        MusicPlayerPlus.Interface.Base.Action.Input.IVolumeChangeAction,
        MusicPlayerPlus.Interface.Base.Action.Input.IPlayStateChangeAction,
        MusicPlayerPlus.Interface.Base.Action.Input.IOrientationActionChange,
        //Output
        MusicPlayerPlus.Interface.Base.Action.Output.IPlayAction,
        MusicPlayerPlus.Interface.Base.Action.Output.IPauseAction,
        MusicPlayerPlus.Interface.Base.Action.Output.IStopAction,
        MusicPlayerPlus.Interface.Base.Action.Output.IPreviousTrackAction,
        MusicPlayerPlus.Interface.Base.Action.Output.INextTrackAction,
        MusicPlayerPlus.Interface.Base.Action.Output.IChangeVolumeAction,
        MusicPlayerPlus.Interface.Base.Action.Output.IOrientationAction
    {
        #region Input

        /// <summary>
        /// 设置主窗体
        /// </summary>
        public void SetMainWindow(object win)
        {
            try
            {
                System.Windows.Window window = win as System.Windows.Window;
                if (window == null)
                {
                    return;
                }
                //播放暂停
                RegisterHotKey(window, System.Windows.Forms.Keys.Space, () =>
                {
                    if (mediaPlayState == MediaPlayState.Play)
                    {
                        Pause_Action();
                    }
                    else
                    {
                        Play_Action();
                    }
                });
                //停止
                RegisterHotKey(window, System.Windows.Forms.Keys.S, () =>
                {
                    Stop_Action();
                });
                //上一首
                RegisterHotKey(window, System.Windows.Forms.Keys.Left, () =>
                {
                    PreviousTrack_Action();
                });
                //下一首
                RegisterHotKey(window, System.Windows.Forms.Keys.Right, () =>
                {
                    NextTrack_Action();
                });


                //音量+
                RegisterHotKey(window, System.Windows.Forms.Keys.Up, () =>
                {
                    double val = volume + 0.1;
                    if (val > 1)
                    {
                        val = 1;
                    }
                    ChangeVolume_Action(val);
                });

                //音量-
                RegisterHotKey(window, System.Windows.Forms.Keys.Down, () =>
                {
                    double val = volume - 0.1;
                    if (val < 0)
                    {
                        val = 0;
                    }
                    ChangeVolume_Action(val);
                });

                //音量-
                RegisterHotKey(window, System.Windows.Forms.Keys.X, () =>
                {
                    OrientationAction_Action("MainWindow", "showorhide", this);
                });
            }
            catch (Exception ex)
            {
            }
        }

        /// <summary>
        /// (改变后事件)音量改变
        /// </summary>
        public void VolumeChange(double val)
        {
            volume = val;
        }

        /// <summary>
        /// 播放状态改变
        /// </summary>
        public void PlayStateChange(MediaPlayState val)
        {
            mediaPlayState = val;
        }

        /// <summary>
        /// 定向通道方法Key
        /// </summary>
        public string OrientationAction_Key { get { return "MainWindowControl"; } }
        /// <summary>
        /// 定向通道方法
        /// </summary>
        public void OrientationActionChange(string key, object obj)
        {
        }

        #endregion

        #region Output

        /// <summary>
        /// 播放
        /// </summary>
        public System.Action Play_Action { private get; set; }

        /// <summary>
        /// 暂停
        /// </summary>
        public System.Action Pause_Action { private get; set; }

        /// <summary>
        /// 停止
        /// </summary>
        public System.Action Stop_Action { private get; set; }

        /// <summary>
        /// 上一首
        /// </summary>
        public System.Action PreviousTrack_Action { private get; set; }

        /// <summary>
        /// 下一首
        /// </summary>
        public System.Action NextTrack_Action { private get; set; }

        /// <summary>
        /// (主动)音量改变
        /// </summary>
        public Action<double> ChangeVolume_Action { private get; set; }

        /// <summary>
        /// 触发定向通道
        /// </summary>
        public Action<string, string, object> OrientationAction_Action { private get; set; }

        #endregion

        #region Method

        /// <summary>
        /// 注册热键
        /// </summary>
        /// <param name="window"></param>
        /// <param name="key"></param>
        /// <param name="action"></param>
        private void RegisterHotKey(System.Windows.Window window, System.Windows.Forms.Keys key, Action action)
        {
            AMing.Core.Method.HotKey hotKey =
                new AMing.Core.Method.HotKey(
                    window, AMing.Core.Method.HotKey.KeyFlags.MOD_Alt |
                    AMing.Core.Method.HotKey.KeyFlags.MOD_Shift, key);
            hotKey.OnHotKey += new AMing.Core.Method.HotKey.OnHotKeyEventHandler(action);
        }

        #endregion

        #region Object

        double volume = 1;
        MediaPlayState mediaPlayState = MediaPlayState.End;


        #endregion

    }
}
